﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using DWEIP.Models.Sys;
using DWEIP.BLL.Sys;
using DWEIP.Common;

namespace DWEIP.BLL.Core
{
    /// <summary>
    /// 写入一个异常错误
    /// </summary>
    /// <param name="ex">异常</param>
    public static class ExceptionHander
    {
        /// <summary>
        /// 加入异常日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void WriteException(Exception ex)
        {
            SysConfigModel siteConfig = new SysConfigBLL().loadConfig(Utils.GetXmlMapPath("Configpath"));
            //后台异常开启
            if (siteConfig.exceptionstatus == 1)
            {
                string subFold = DateTime.Now.Year + DateTime.Now.Month.ToString("D2");
                string fileName = subFold + DateTime.Now.Day.ToString("D2") + ".txt";
                string path = System.Web.HttpContext.Current.Server.MapPath("~/LogFile/") + subFold;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string wholePath = path + "\\" + fileName;
                using (StreamWriter sw = new StreamWriter(wholePath, true, Encoding.UTF8))
                {
                    sw.WriteLine((ex.Message + "|" + ex.StackTrace + "|" + ex.Message + "|" + DateTime.Now.ToString()).ToString());
                    sw.Dispose();
                    sw.Close();
                }
                return;
            }

        }

    }
}
